/*
 *
 * Copyright 2007, 2008 Tiberiumlabs
 *
 * This file is part of Tiberiumlabs Lailaps.
 *
 * Tiberiumlabs Lailaps is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Tiberiumlabs Lailaps is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.tiberiumlabs.lailaps.gui.elements.ui;

import java.awt.Color;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.UIManager;
import javax.swing.text.JTextComponent;

/**
 *
 * @author <a href="paranoid.tiberiumlabs@gmail.com">Paranoid</a>
 */
public class TextFieldFocusListener implements FocusListener {

    private static final TextFieldFocusListener LISTENER = new TextFieldFocusListener();

    public static TextFieldFocusListener sharedInstance() {
        return LISTENER;
    }

    private TextFieldFocusListener() {
    }

    private static Color FOCUSED_BACKGROUND_COLOR = null;
    private static Color NORMAL_BACKGROUND_COLOR = null;
    static {
        if (FOCUSED_BACKGROUND_COLOR == null) {
            Color focusedColor = UIManager.getColor("TextField.focused");
            if (focusedColor != null) {
                FOCUSED_BACKGROUND_COLOR = focusedColor;
            }
            else {
                FOCUSED_BACKGROUND_COLOR = new Color(255, 255, 220);
            }

            Color normalColor = UIManager.getColor("TextField.background");
            if (normalColor != null) {
                NORMAL_BACKGROUND_COLOR = normalColor;
            }
            else {
                NORMAL_BACKGROUND_COLOR = Color.WHITE;
            }
        }
    }

    public void focusGained(FocusEvent e) {
        JTextComponent textComponent = (JTextComponent)e.getComponent();
        if (textComponent.isEnabled() && textComponent.isEditable()) {
            textComponent.setBackground(FOCUSED_BACKGROUND_COLOR);
        }
    }

    public void focusLost(FocusEvent e) {
        JTextComponent textComponent = (JTextComponent)e.getComponent();
        if (textComponent.isEnabled() && textComponent.isEditable()) {
            textComponent.setBackground(NORMAL_BACKGROUND_COLOR);
        }
    }

}
